//using CoreCms.Net.Identity.Authorization.Handlers;
//using CoreCms.Net.Identity.Authorization.Requirements;
//using JetBrains.Annotations;
//using Microsoft.AspNetCore.Authorization;
//using Microsoft.Extensions.Options;
//using Newtonsoft.Json;

//namespace CoreCms.Net.Identity.Authorization.Providers;

//public sealed class SilkyAuthorizationPolicyProvider : IAuthorizationPolicyProvider
//{
//    //const string PolicyPrefix = "Permission:";

//    public DefaultAuthorizationPolicyProvider FallbackPolicyProvider { get; }

//    public SilkyAuthorizationPolicyProvider(IOptions<AuthorizationOptions> options)
//    {
//        // ASP.NET Core only uses one authorization policy provider, so if the custom implementation
//        // doesn't handle all policies (including default policies, etc.) it should fall back to an
//        // alternate provider.
//        //
//        // In this sample, a default authorization policy provider (constructed with options from the
//        // dependency injection container) is used if this custom provider isn't able to handle a given
//        // policy name.
//        //
//        // If a custom policy provider is able to handle all expected policy names then, of course, this
//        // fallback pattern is unnecessary.
//        FallbackPolicyProvider = new DefaultAuthorizationPolicyProvider(options);
//    }

//    public Task<AuthorizationPolicy> GetDefaultPolicyAsync() => FallbackPolicyProvider.GetDefaultPolicyAsync();

//    // For ASP.NET Core 3.0
//    //public Task<AuthorizationPolicy> GetFallbackPolicyAsync() => FallbackPolicyProvider.GetFallbackPolicyAsync();

//    public Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
//    {
//        var authorizeData = JsonConvert.DeserializeObject<PermissionAuthorizeData>(policyName);
//        var policy = new AuthorizationPolicyBuilder();
//        policy.AddRequirements(new SilkyAuthorizationHandlerBase(authorizeData));
//        return Task.FromResult(policy.Build());

//        //if (policyName.StartsWith(PolicyPrefix, StringComparison.OrdinalIgnoreCase))
//        //{
//        //    var policyValue = policyName.Substring(PolicyPrefix.Length);
//        //    var authorizeData = JsonConvert.DeserializeObject<PermissionAuthorizeData>(policyValue);
//        //    var policy = new AuthorizationPolicyBuilder();
//        //    policy.AddRequirements(new SilkyAuthorizationHandlerBase(authorizeData));
//        //    return Task.FromResult(policy.Build());
//        //}

//        // If the policy name doesn't match the format expected by this policy provider,
//        // try the fallback provider. If no fallback provider is used, this would return
//        // Task.FromResult<AuthorizationPolicy>(null) instead.
//        //return FallbackPolicyProvider.GetPolicyAsync(policyName);
//    }

//    public Task<AuthorizationPolicy?> GetFallbackPolicyAsync()
//    {
//        return FallbackPolicyProvider.GetFallbackPolicyAsync();
//    }
//}